package com.feng.dt;

import com.feng.common.web.WebCommonResponseBuilder;
import com.feng.profile.model.entity.SysUser;
import com.feng.profile.model.enums.StatusEnum;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.junit.Test;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author Lee
 */
public class test {

    @Test
    public void applePriceSub(){
        DecimalFormat decimalFormat = new DecimalFormat("0.00");

        BigDecimal count = new BigDecimal(100);
        BigDecimal price = new BigDecimal(2.05);
        BigDecimal sum = price.multiply(count);
        String format = decimalFormat.format(sum);
        System.out.println(format);
    }

    @Test
    public void sortStudentAge(){
        List<Student> studentList  =
                Arrays.asList(new Student("AA",4),new Student("AA",40),new Student("AA",20),new Student("AA",30));
        studentList.stream()
                .sorted((s1,s2) -> Integer.compare(s1.getAge(),s2.getAge()));
       /* studentList .sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });*/
        System.out.println(studentList );
    }

    @Test
    public void arrayTest(){
        int b[][] = {
                {1,2,3},{4,5},{6,7,8}
        };
        System.out.println(b.length);
        System.out.println(b[1].length);
    }

    @Test
    public void converterTest(){
        new Thread(new testThreadStop()).start();
        test.staticMethodTest();
    }

    @Test
    public void threadCount(){
        System.out.println(Runtime.getRuntime().availableProcessors());
    }

    @Test
    public void atomicTest(){
        AtomicReference<Integer> atomicReference = new AtomicReference<Integer>(100);
        AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(100,1);
        atomicStampedReference.compareAndSet(100,200,1,2);
        System.out.println(atomicStampedReference.getStamp());
        System.out.println(atomicStampedReference.getReference());
    }

    @Test
    public void toStringTest(){
        SysUser sysUser = new SysUser();
        sysUser.setStatus(StatusEnum.E);
        sysUser.setUserId("12312lskdjf");
        sysUser.setCreateTime(new Date());
        System.out.println(sysUser);

        System.out.println(new WebCommonResponseBuilder<SysUser>().success().build(sysUser));
    }

    @Test
    public void sortTest(){
        int[] a = {4,3,2,1};
        //1 {3,2,1,4}
        //2 {2,1,3,4}
        //3 {1,2,3,4}

        int[] arr = {3213, 32, 432, 1};
        String str = "abc";
        String str2 = "ABC";
        System.out.println(str.concat(str2));
        int[] intSort = intSort(arr);
        for (int i1 : intSort) {
            System.out.println(i1);
        }
    }

    private int[] intSort(int[] arr){
        for (int i = 0;i < arr.length ; i++) {
            for (int index = 0; index < arr.length - 1; index++) {
                int tem;
                if (arr[index] > arr[index +1]) {
                    tem = arr[index];
                    arr[index] = arr[index + 1];
                    arr[index + 1] = tem;
                }
            }
        }
        return arr;
    }

    @Test
    public void selfIncrementTest(){
        Integer i = 1;
        i = i++;
        int j = i++;
        System.out.println(i + "," + i++ + "," + ++i);
        System.out.println(i + i++ * ++i);
        int k = i + ++i * i++;
        System.out.println(k);
        System.out.println(i);
    }

    @Test
    public void stringIndexToUpperCase() {
        String s = "asd@fj_asld";
        System.out.println(dbName2ClassName(s));
//        (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();

    }

    String dbName2ClassName(String dbName) {
        String s = dbName;

        boolean allUpperCaseOrNumeric = true;
        for (char c : s.toCharArray()) {
            if ((c != '_') && (!CharUtils.isAsciiNumeric(c)) && (!CharUtils.isAsciiAlphaUpper(c))) {
                allUpperCaseOrNumeric = false;
                break;
            }
        }
        if (allUpperCaseOrNumeric) {
            s = s.toLowerCase();
            s = WordUtils.capitalizeFully(s, new char[]{'_'});
            s = StringUtils.remove(s, "_");
        }
        if (!StringUtils.isAlpha(StringUtils.left(s, 1))) {
            s = "_" + s;
        }
        s = s.substring(0,1).toUpperCase() + s.substring(1);
        return s;
    }

    static void staticMethodTest(){
        test t = new test();
        t.sortTest();
        long l =499;
//        int i =4L;
        float f =1.103123f;
        double d =34.4;

    }

}

class testThreadStop implements Runnable{

    @Override
    public void run() {

        System.out.println("123123123123");
    }
}

class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
